package com.example.timeemail.letter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.timeemail.MainActivity;
import com.example.timeemail.R;
import com.example.timeemail.RegisterActivity;
import com.example.timeemail.SigninActivity;
import com.example.timeemail.entity.Letter;
import com.example.timeemail.utils.LoginResult;
import com.example.timeemail.utils.OkHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class EmailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);
    }

    public void send(View view){
        //发送内容检查 是否为空 是否格式正确

        Letter letter = new Letter();
        EditText editTextTextLine = findViewById(R.id.editTextTextLine);
        EditText editTextTime2 = findViewById(R.id.editTextTime2);
        EditText editTextTextMultiLine = findViewById(R.id.editTextTextMultiLine);
        EditText editTextTextEmailAddress2 = findViewById(R.id.editTextTextEmailAddress2);
        CheckBox checkBox = findViewById(R.id.checkBox);

        //获取信件名称
        letter.setCoursename(editTextTextLine.getText().toString());
        //发送者
        SharedPreferences sharedPreferences = this.getSharedPreferences("loginData", MODE_PRIVATE);
        String username = sharedPreferences.getString("username", null);
        String email = sharedPreferences.getString("email", null);
        if (username != null) {
            letter.setTeacherid(username);
        }else if (email != null) {
            letter.setTeacherid(email);
        }
        //发送时间
        letter.setCoursetime(editTextTime2.getText().toString());
        //信件内容
        letter.setClassroom(editTextTextMultiLine.getText().toString());
        //收件人邮箱
        letter.setCourseweek(editTextTextEmailAddress2.getText().toString());
        //是否公开
        boolean activated = checkBox.isChecked();
        letter.setScore(activated ?"是":"否");
        saveLetterWithOkHttp(letter);
    }

    void ToastShow(String context){
        Toast toast = Toast.makeText(this, context, Toast.LENGTH_SHORT);
        View view = LayoutInflater.from(this).inflate(R.layout.toast_template,null);
        TextView TxtTaostContext = (TextView)view.findViewById(R.id.TxtTaostContext);
        TxtTaostContext.setText(context);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }

    //保存信件内容
    private void saveLetterWithOkHttp(Letter letter) {
        OkHttpUtil.saveLetterWithOkHttp(letter, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("EmailActivity", "保存信件内容请求失败");
                ToastShow("保存信件内容请求失败：" + e.getLocalizedMessage());
            }

            //请求成功，获取服务器返回数据
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //获取返回内容
                final String responseData = response.body().string();
                //在主线程更新ui和响应用户操作
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LoginResult result = new Gson().fromJson(responseData, LoginResult.class);
                        if (result.getStatus().equals("200")) {
                            ToastShow("保存信件成功");
                            Intent intent = new Intent(EmailActivity.this, MainActivity.class);
                            startActivity(intent);
                            /*
                            Intent intent=new Intent();
                            intent.putExtra("id",etId.getText().toString());
                            intent.putExtra("email",etEmail.getText().toString());
                            intent.putExtra("password",etPassword.getText().toString());
                            setResult(0,intent);*/
                            finish();
                        } else {
                            Toast.makeText(EmailActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}